﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

namespace Frame.Lib.Data
{
    public abstract partial class DBHelper
    {
        protected IDbConnection _conn;
        public abstract string ConnectionString
        {
            get;
            set;
        }

        public abstract DbTransaction Transaction
        {
            get;
        }

        #region 打开连接
        /// <summary>
        /// 打开连接
        /// </summary>
        private void Open()
        {
            if (_conn.State != ConnectionState.Open)
            {
                _conn.Open();
            }
        }
        #endregion

        #region 关闭连接
        /// <summary>
        /// 关闭连接
        /// </summary>
        private void Close()
        {
            if (_conn.State != ConnectionState.Closed)
            {
                _conn.Close();
            }
        }
        #endregion


        public abstract int RunSql(string sql, params object[] paras);

        /// <summary>
        /// 带事务处理
        /// </summary>
        /// <param name="sqls"></param>
        /// <returns></returns>
        public abstract int RunSql(string[] sqls, params object[] paras);
        public abstract DataTable Query(string sql, params object[] paras);
        public abstract List<Info> Query<Info>(string sql, params object[] paras);
        public abstract void RunProc(string sql, params object[] paras);
        public abstract T GetScalar<T>(string sql, params object[] paras);

        public abstract void BeginTrans();

        public abstract void CommitTrans();

        public abstract void RollbackTrans();


        public static DBHelper Instance()
        {
            string connectionString = ConnectionStringManager.Get(System.Configuration.ConfigurationManager.ConnectionStrings["Default"].ConnectionString);
            return new SqlDBHelper(connectionString);
        }

        public static DBHelper Instance(string connectionString)
        {
            connectionString = ConnectionStringManager.Get(connectionString);
            return new SqlDBHelper(connectionString);
        }
    }
}
